package com.hoe.tsb.web;

import com.hoe.tsb.Configuration;
import com.hoe.tsb.fraft.client.FRaftClient;
import com.hoe.tsb.fraft.client.SimpleClient;
import com.hoe.tsb.web.bean.EarlyWarning;
import com.hoe.tsb.web.bean.R;
import com.hoe.tsb.web.bean.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 对客户端提供服务的Api网关
 * </p>
 *
 * @author hoe
 * @version 1.0
 * @date 2022/11/24 11:42
 */
@RestController
@RequestMapping("/tsb")
public class TsbApi {

    @Autowired
    private NodeProperties properties;

    //FRaft客户端
    private FRaftClient mFRaftClient;

    @PostConstruct
    public void init() {
        mFRaftClient = SimpleClient.getClient(properties, Configuration.getDefault());
    }

    /**
     * 获取最顶端的区块
     * @return 顶端区块数据
     */
    @GetMapping("/ew")
    public R<?> getEarlyWarning(String traceHashes) {
        if (StringUtils.isEmpty(traceHashes)) {
            return R.success(new ArrayList<EarlyWarning>(0));
        }

        List<EarlyWarning> list = Arrays.stream(traceHashes.split(",")).map(t -> {
            if ("ae5bf9f177880a98defe30b06668fe3632157e85d9d28b544f6edeaee39fad52".equals(t) || "3f649103e89c7eef49204427928919f8dce18a8d9e99090caad61a6958cd3d86".equals(t)) {
                EarlyWarning ew = new EarlyWarning();
                ew.setTraceHash(t);
                ew.setScore("ae5bf9f177880a98defe30b06668fe3632157e85d9d28b544f6edeaee39fad52".equals(t) ? 65 : 78);
                return ew;
            } else {
                return null;
            }
        }).filter(Objects::nonNull).collect(Collectors.toList());
        return R.success(list);
    }

    /**
     * 获取最顶端的区块
     * @return 顶端区块数据
     */
    @GetMapping("/top")
    public R<?> queryTopBlock() {
        return R.success(mFRaftClient.getTopBlock());
    }

    /**
     * 根据区块Hash查询区块数据
     * @param blockHash 区块Hash
     * @return 区块数据
     */
    @GetMapping("/block")
    public R<?> blockQuery(String blockHash) {
        return R.success(mFRaftClient.getBlock(blockHash));
    }

    /**
     * 根据溯源Hash查询区块Hash
     * @param traceHash 溯源Hash
     * @return 区块Hash
     */
    @GetMapping("/block/hash")
    public R<?> blockHashQuery(String traceHash) {
        return R.success(mFRaftClient.getBlockHash(traceHash));
    }

    /**
     * 查询当前区块链的块高
     * @return 块高
     */
    @GetMapping("/height")
    public R<?> heightQuery() {
        return R.success(mFRaftClient.getHeight());
    }

    /**
     * 提交溯源数据<br/>
     * TODO 溯源链的权限系统属于强业务功能，暂不予实现，故本接口暂不校验权限<br/>
     * @param trace 溯源数据<br/>
     *              格式如下：<br/>
     *              parentTrace：父溯源数据的Hash
     * @return 溯源hash
     */
    @PostMapping("/submit")
    public R<?> submitTrace(@RequestBody Trace trace) {
        Object result = mFRaftClient.submit(trace);
        return result == null ? R.successEmpty() : R.success(result);
    }

}
